package com.xilihui.xlh.business.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.LoginEntity;
import com.xilihui.xlh.business.requests.LoginRequest;
import com.xilihui.xlh.core.app.BaseApplication;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.SPUtil;
import com.xilihui.xlh.core.util.TextExtend;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.feezu.liuli.timeselector.Utils.TextUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/5/28.
 */

public class LoginActivity extends BaseCompatActivity implements View.OnClickListener {
    
    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.et_passwd)
    EditText et_passwd;
    @BindView(R.id.tv_login)
    TextView tv_login;
    
    private String phone = "";
    private String passwd = "";
    
    @OnClick(R.id.iv_close)
    public void close(){
        finish();
    }
    
    @Override
    public int getLayout() {
        return R.layout.activity_login;
    }
    
    @Override
    public String initTitle() {
        return null;
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        tv_login.setOnClickListener(this);
        
        phone = (String) SPUtil.get(LoginActivity.this,SPUtil.PHONE,"");
        et_phone.setText(phone);
        if(!TextUtil.isEmpty(phone)){
            et_phone.setSelection(phone.length());
        }
        
    }

    @Override
    public void initData() {
        
    }
    
    @OnClick(R.id.tv_forget)
    public void forget(){
        startActivity(new Intent(LoginActivity.this,ForgetActivity.class));
    }
    
    @OnClick(R.id.tv_reg)
    public void reg(){
        startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
    }
    
    
    public void login(){
        
        phone = et_phone.getText().toString().trim();
        passwd = et_passwd.getText().toString().trim();
        
        if(TextUtils.isEmpty(phone)){
            ToastUtil.toastShortNegative("请输入手机号码");
            return;
        }
        
        if(!TextExtend.isMobile(phone)){
            ToastUtil.toastShortNegative("请输入正确的手机号码");
            return;
        }
        
        if(TextUtils.isEmpty(passwd)){
            ToastUtil.toastShortNegative("请输入密码");
            return;
        }
        
        if(passwd.length()<6||passwd.length()>16){
            ToastUtil.toastShortNegative("请输入6~20位密码");
            return;
        }

        LoginRequest.login(LoginActivity.this,phone,passwd)
                .compose(DoTransform.<LoginEntity>applyScheduler())
                .compose(LoginActivity.this.<LoginEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<LoginEntity>(LoginActivity.this) {
                    @Override
                    protected void onSuccess(LoginEntity entity) {
                        ToastUtil.toastShortPositive(entity.getMsg());
                        SPUtil.put(LoginActivity.this,SPUtil.TOKEN,entity.getAccess_token());
                        SPUtil.put(LoginActivity.this,SPUtil.USER_ID,entity.getUser_id());
                        SPUtil.put(LoginActivity.this,SPUtil.NICK,entity.getNickname());
                        SPUtil.put(LoginActivity.this,SPUtil.IS_LOGIN,true);
                        SPUtil.put(LoginActivity.this,SPUtil.HEAD_IMAGE,entity.getHead_pic());
                        
                        SPUtil.put(BaseApplication.get(), SPUtil.IS_LOGIN_ACTIVITY_OPEN,false);
                        SPUtil.put(BaseApplication.get(),SPUtil.PHONE,phone);
                        
                        YEventBuses.post(new YEventBuses.Event("login_"));
                        
                        Intent intent = new Intent(LoginActivity.this,HomeActivity.class);
                        intent.putExtra("logout",2);
                        startActivity(intent);
                        finish();
                        
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
        
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_login:
                login();
                break;
        }
    }
    
    @OnClick(R.id.tv_smsLogin)
    public void smsLogin(){
        startActivity(new Intent(LoginActivity.this,SmsLoginActivity.class));
        finish();
    }
    
}
